// pages/store/store.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    imageTitle: '',
    oneImage: null,
    idcode_positive: [],
    idcodeList: [],
    startTime: '00:00',
    endTime: '00:00',
    business: '',
    statusd: 1,
    valueInput: '',
    title: '',
    markers: [{
      iconPath: "../images/adres.png",
      id: 0,
      latitude: 23.099994,
      longitude: 113.324520,
      width: 30,
      height: 30
    }],
    buttState:1
  },
  onLoad: function(options) {
    var _this = this
    _this.onInfoList()
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 1500)
  },
  jjnput(e) {
    var _this = this,
      id = e.currentTarget.id
    if (id == 1) {
      _this.setData({
        valueInput: e.detail.value
      })
    } else if(id == 2){
      var ind = _this.data.info
      ind.title = e.detail.value
      _this.setData({
        info: ind
      })
    }else{
      var ind = _this.data.info
      ind.supplier.detailed_address = e.detail.value
      _this.setData({
        info: ind
      })  
    }

  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  // onShareAppMessage: function() {},
  onInfoList() {
    var _this = this
    com.link(app.link.nsn + wx.getStorageSync('store') + '.st', {}, function(res) {
      console.log(res) 
      var info = res.data.data
      if (info.operate) {
        var end = info.operate.substring(6, 11)
        info.operate = info.operate.substring(0, 5)
        _this.setData({
          startTime: info.operate,
          endTime: end
        })
      }
      var markers = _this.data.markers
      markers[0].latitude = info.latitude
      markers[0].longitude = info.longitude
      _this.setData({
        info: info,
        business: info.business,
        idcode_positive: info.media,
        oneImage: info.trademark_url,
        valueInput: info.content || '',
        buttState: 0,
        markers: markers,
      })
      _this.onlisy()
      console.log(_this.data)
    }, 'GET')
  },
  //防止深度拷贝模式~
  onlisy() {
    var _this = this
    com.link(app.link.nsn + +wx.getStorageSync('store') + '.st', {}, function(res) {
      console.log(res)
      _this.setData({
        idcodeList: res.data.data.media,
      })
      console.log(_this.data)
    }, 'GET')
  },
  bindTimeChange(e) {
    console.log(e)
    var id = e.currentTarget.id,
      _this = this
    if (id == 1) {
      _this.setData({
        startTime: e.detail.value
      })
    } else {
      _this.setData({
        endTime: e.detail.value
      })
    }
  },
  onOptometrist() {
    wx.navigateTo({
      url: '/pages/Optometrist/Optometrist',
    })
  },
  onUpdateImage(e) {
    var id = e.currentTarget.id,
      _this = this
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        if (id == 1) {
          _this.setData({
            oneImage: res.tempFilePaths[0]
          })
        } else if (id == 2) {
          let jsone = {},
            list = _this.data.idcode_positive
          jsone.id = -1
          jsone.url = res.tempFilePaths[0]
          list.push(jsone)
          _this.setData({
            idcode_positive: list
          })
        }
        console.log(_this.data)
        wx.uploadFile({
          url: app.link.temporary,
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            file: res.tempFilePaths[0]
          },
          success: function(res) {
            console.log(res)
            let img = JSON.parse(res.data)
            console.log(img)
            if (id == 1) {
              _this.setData({
                imageTitle: img.response.file.name
              })
            } else {
              let idcodeList = _this.data.idcodeList,
                jsons = {}
              jsons.id = -1
              jsons.url = img.response.file.name
              idcodeList.push(jsons)
              _this.setData({
                idcodeList: idcodeList
              })
            }
          }
        })
      }
    })
  },
  //确定按钮
  onComet() {
    wx.showLoading({
      title: '加载中',
    })
    var _this = this,
      list = _this.data.idcodeList,
      s = []
    if (_this.data.buttState == 1){
      _this.setData({
        buttState:0,
      })
    }else{
      wx.hideLoading()

      return 
    }
    list.map((v, k) => {
      if (list[k].id == -1) {
        console.log(list[k].url)
        s.push(list[k].url)
      }
    })
    com.link(app.link.nsn + wx.getStorageSync('store') + '.st', {
      operate: _this.data.startTime + '-' + _this.data.endTime,
      business: _this.data.business == true ? 1 : 0,
      trademark: _this.data.imageTitle,
      media: s,
      content: _this.data.valueInput,
      title: _this.data.info.title,
      address: _this.data.info.supplier.detailed_address
    }, function(res) {
      console.log(res)
      if (res.data.state) {
        _this.setData({
          imageTitle: '',
        })
        wx.showToast({
          title: '更新成功',
        })
        setTimeout(() => {
          _this.onInfoList()
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.message,
        })
      }
      wx.hideLoading()
    })
  },
  //是否营业
  baseswitch(e) {
    console.log(e)
    var _this = this
    if (e.detail.value.length != 0) {
      _this.setData({
        business: 1
      })
    } else {
      _this.setData({
        business: 0
      })
    }
  },
  //删除照片
  deleteImage(e) {
    console.log(e)
    var _this = this,
      id = e.currentTarget.id,
      list = _this.data.idcode_positive,
      listinfo = _this.data.idcodeList
    if (id == 1) {
      _this.setData({
        oneImage: null,
        imageTitle: ''
      })
    } else {
      if (e.currentTarget.dataset.id != -1) {
        com.link(app.link.store_a + e.currentTarget.dataset.id + '.st', {}, function(res) {
          console.log(res);
          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })
          list.splice(e.currentTarget.dataset.index, 1);
          listinfo.splice(e.currentTarget.dataset.index, 1);
          _this.setData({
            idcode_positive: list,
            idcodeList: listinfo
          })
        })
        return false;
      }

      list.splice(e.currentTarget.dataset.index, 1);
      listinfo.splice(e.currentTarget.dataset.index, 1);
      _this.setData({
        idcode_positive: list,
        idcodeList: listinfo
      })
    }
  },
})